package com.ruoyi.web.controller.kpl.other;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * 热门主题
 */
@RestController
@RequestMapping("/topic")
public class HotTopicController {

    /**
     * 热门主题 左侧的
     * @param request
     * @return
     */
    @GetMapping("/left")
    public String left(HttpServletRequest request) throws JsonProcessingException {
        Map<String,Object> params = new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        String url = null;
        String tt = null;//日期
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = request.getParameter(name);
            if(name.equals("url")){
                url = value;
                continue;
            }
            if(name.equals("tt")){
                tt = value;
                continue;
            }
            params.put(name,value);
        }

        String now = DateUtils.now("yyyy-MM-dd");
        String data = null;
        String lastDateData = null;//上一个工作日 日期题材
        //获取当天题材
        if(now.equals(tt)){
             data = HttpUtils.getData(url, params);
        }else{
            //获取指定日期的题材
            data = FileUtils.read(Constant.filePath + "files/热门题材/"+tt+"/left_"+tt+".txt");
        }

        //tt指定日期的上一个日期时间
        String lastTT = DateUtils.lastWorkDate(tt);
        lastDateData = FileUtils.read(Constant.filePath + "files/热门题材/"+lastTT+"/left_"+lastTT+".txt");

        if(data == null || data.trim().length()==0){ //如果获取不到数据 则还是请求当天的数据
            data = HttpUtils.getData(url, params);
        }

        List<String> lastCodes = new ArrayList<>();
        try{
            lastCodes = getLeftCodes(lastDateData);
        }catch (Exception e){
            e.printStackTrace();
        }
        List<String> codes = getLeftCodes(data);

        // 创建ObjectMapper实例
        ObjectMapper mapper = new ObjectMapper();

        // 将JSON字符串解析为JsonNode
        JsonNode rootNode = mapper.readTree(data);

        // 获取list数组
        ArrayNode listArray = (ArrayNode) rootNode.get("list");

        // 遍历list数组中的每个内部数组
        for (int i = 0; i < listArray.size(); i++) {
            ArrayNode innerArray = (ArrayNode) listArray.get(i);
            String code = innerArray.get(0).toString().replace("\"","");

            //判断是否是新题材板块
            if(lastCodes.contains(code)){
                innerArray.add(0);//不是新的
                int lastIndex = lastCodes.indexOf(code);
                int codeIndex = codes.indexOf(code);
                if(codeIndex<lastIndex){
                    innerArray.add(2);//排序上升了
                }
                if(codeIndex>lastIndex){
                    innerArray.add(1);//排序下降
                }
                if(codeIndex == lastIndex){
                    innerArray.add(0);//没有变化
                }
            }else{
                innerArray.add(1);//是新的
                innerArray.add(0);//排序没有变化
            }


        }

        String compactJson = mapper.writeValueAsString(rootNode);

        return compactJson;
    }

    /**
     * 获取左侧的板块code
     * @param data
     * @return
     */
    public static List<String> getLeftCodes(String data) throws JsonProcessingException {
        // 创建ObjectMapper实例
        ObjectMapper mapper = new ObjectMapper();

        // 将JSON字符串解析为JsonNode
        JsonNode rootNode = mapper.readTree(data);

        // 获取list数组
        ArrayNode listArray = (ArrayNode) rootNode.get("list");

        List<String> codes = new ArrayList<>();
        // 遍历list数组中的每个内部数组
        for (int i = 0; i < listArray.size(); i++) {
            ArrayNode innerArray = (ArrayNode) listArray.get(i);
            String code = innerArray.get(0).toString().replace("\"","");

            codes.add(code);
        }

        return codes;
    }

    /**
     * 改造热门题材【精选板块 内容】修改原有的json数据 结合本地选板块做比对 如果是本地板块内的 则添加指定样式提示 方便查看
     * @param request
     * @return
     */
    @GetMapping("/right")
    public String right(HttpServletRequest request) throws Exception{
        Map<String,Object> params = new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        String url = null;
        String tt = null;
        String PlateID = null;
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            String value = request.getParameter(name);
            if(name.equals("url")){
                url = value;
                continue;
            }

            if(name.equals("tt")){
                tt = value;
                continue;
            }

            if(name.equals("PlateID")){
                PlateID = value;
            }

            params.put(name,value);
        }

        String now = DateUtils.now("yyyy-MM-dd");
        String data = null;

        if(now.equals(tt)){
            data = HttpUtils.getData(url, params);
        }else{
            data = FileUtils.read(Constant.filePath + "files/热门题材/"+tt+"/right_"+PlateID+".txt");
        }

        if(data == null){ //如果获取不到数据 则还是请求当天的数据
            data = HttpUtils.getData(url, params);
        }


        String pathQLSB = Constant.filePath + "T0002/blocknew/QLSB.blk";

        List<String> codes = new ArrayList<>();
        BufferedReader brQLSB = new BufferedReader(new FileReader(pathQLSB));

        String line = null;

        while ((line = brQLSB.readLine()) != null) {
            if(line.trim().length()>1){
                codes.add(line.substring(1));
            }
        }

        brQLSB.close();

        List<String> codesHot = hots();

        // 创建ObjectMapper实例
        ObjectMapper mapper = new ObjectMapper();

        // 将JSON字符串解析为JsonNode
        JsonNode rootNode = mapper.readTree(data);

        // 获取list数组
        ArrayNode listArray = (ArrayNode) rootNode.get("list");

        // 遍历list数组中的每个内部数组
        for (int i = 0; i < listArray.size(); i++) {
            ArrayNode innerArray = (ArrayNode) listArray.get(i);
            String code = innerArray.get(0).toString().replace("\"","");

            //如果在擒龙首板中有 则加1 否则是2
            if(codes.contains(code)){
                innerArray.add("1");//是首板推荐的 擒龙首板
            }else{
                innerArray.add("2");//不是首板推荐的
            }

            if(codesHot.contains(code)){
                innerArray.add("3");//是热榜
            }else{
                innerArray.add("4");//不是热榜
            }

        }

        String compactJson = mapper.writeValueAsString(rootNode);
        return compactJson;
    }


    private  List<String> hots() throws IOException {
        List<String> codesHot = new ArrayList<>();
        String pathHot = Constant.filePath +"files/LAB.txt";//热榜

        BufferedReader brHot = new BufferedReader(new FileReader(pathHot));

        String lineHot = null;

        while ((lineHot = brHot.readLine()) != null) {
            codesHot.add(lineHot);
        }

        brHot.close();

        return codesHot;
    }



}
